#include "StdAfx.h"
#include "channel.h"
#include "User.h"
#include "UserManager.h"
#include "ChannelManager.h"
#include "ServerSession.h"

Channel::Channel(void)
{
    m_pServerSession = NULL;
	m_dwVillageUserNum = 0;
	m_dwLobbyUserNum = 0;
	m_dwMissionUserNum = 0;
	m_dwHuntingUserNum = 0;
	m_dwQuestUserNum = 0;
	m_dwPVPUserNum = 0;
	m_dwEventUserNum = 0;
	m_dwGuildUserNum = 0;
	m_dwSiegeUserNum = 0;

	m_ChatRoomKeyGenerator.Create( 1, 10000 );	
}

Channel::~Channel(void)
{
	
}

VOID Channel::Init()
{
	
}

VOID Channel::Release()
{
	USER_MAP_ITER	it;
	User			*pUser;

	USER_MAP m_mapUsersClone = m_mapUsers;
	for( it = m_mapUsersClone.begin(); it != m_mapUsersClone.end(); ++it )
	{
		pUser = it->second;
		RemoveUser( pUser );
	}
	m_mapUsersClone.clear();

	m_dwVillageUserNum = 0;
	m_dwLobbyUserNum = 0;
	m_dwMissionUserNum = 0;
	m_dwHuntingUserNum = 0;
	m_dwQuestUserNum = 0;
	m_dwPVPUserNum = 0;
	m_dwEventUserNum = 0;
	m_dwGuildUserNum = 0;
	m_dwSiegeUserNum = 0;

}

VOID Channel::Update()
{
}

VOID Channel::AddUser( User *pUser)
{
      if(m_mapUsers.find(pUser->GetGUID())==m_mapUsers.end())
	  {
          m_mapUsers.insert(USER_MAP_PAIR( pUser->GetGUID(), pUser ));
	  }
}
VOID Channel::RemoveUser( User *pUser )
{
	USER_MAP::iterator it = m_mapUsers.find(pUser->GetGUID());
	if(it!=m_mapUsers.end())
	{
		m_mapUsers.erase(it);
	}
}

VOID Channel::Send(BYTE* pMsg,WORD wSize)
{
	if(m_pServerSession)
	{
		m_pServerSession->Send(pMsg,wSize);
	}
}

VOID Channel::SendToAll( BYTE *pMsg, WORD wSize )
{
	USER_MAP_ITER	it;
	User			*pUser;
	for( it = m_mapUsers.begin(); it !=m_mapUsers.end(); ++it )
	{
		pUser = it->second;
		pUser->Send( pMsg, wSize );
	}
}
